package com.kwlt.smc.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

import com.kwlt.smc.run.HttpRunMain;


/**
 * 数据库连接池
 * @author xushunxin
 *
 */
public class DbcpPoolManager {

	private static DataSource source = null;

	public DbcpPoolManager() {

	}

	/**
	 * 初始化连接池
	 */
	public static void initPool() {
		File file = null;
		InputStream in = null;
		try {
			Properties prop = new Properties();
			file = new File("config/dbcp.properties");
			//file = new File("D:\\Workspaces\\http_gw_ld\\config\\dbcp.properties");
			in = new FileInputStream(file);
			prop.load(in);
			source = BasicDataSourceFactory.createDataSource(prop);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(in!=null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *  获得数据库连接
	 * 
	 * @return
	 * @throws SQLException
	 */
	public static synchronized Connection getConnection() throws SQLException {
		if (source == null)
			initPool();
		Connection conn = source.getConnection();
		return conn;
	}
}
